package com.hfengxiang.example.canvasrotatedemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.w3c.dom.ls.LSException;

public class ScreenRotateActivity extends AppCompatActivity {

    private FrameLayout mFlContainer;
    private int last = 1;
    private int rotation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_screen_rotate);
        WindowManager.LayoutParams windowAttributes = getWindow().getAttributes();
        windowAttributes.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS;
        getWindow().setAttributes(windowAttributes);

        initView();
    }

    private void initView() {
        mFlContainer = (FrameLayout) findViewById(R.id.fl_container);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        int orientation = newConfig.orientation;
        Log.v("orientation", "orientation=" + orientation+",last="+ last);
        last = orientation;
        rotation = orientation == 1 ? -90 : 90;
        ScreenRotateView screenRotateView = new ScreenRotateView(this);
        screenRotateView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mFlContainer.addView(screenRotateView);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = animation.getAnimatedFraction();
                Log.d("Hfx", "ww=" + getWindow().getDecorView().getWidth() / 2f + ",wh=" + getWindow().getDecorView().getHeight() / 2f);
                screenRotateView.setProgress(fraction,
                        getWindow().getDecorView().getWidth() / 2f,
                        getWindow().getDecorView().getHeight() / 2f, rotation);
                screenRotateView.invalidate();
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mFlContainer.removeView(screenRotateView);
            }
        });
        valueAnimator.start();
    }
}